package lemon

import java.awt.print.Printable;

import org.apache.commons.lang.Validate;
import org.apache.tools.ant.taskdefs.Concat

import com.axon.lemon.City;
import com.axon.lemon.District;
import com.axon.lemon.Feature;
import com.axon.lemon.Image
import com.axon.lemon.Place
import com.axon.lemon.Contact
import com.axon.lemon.PropertyType;
import com.axon.lemon.UserProfile;


class dataService {
		def getCity(){
			def listCity=City.list();
			return listCity;
		}
		
		def getDistrict(){
			def listDistrict=District.list();
			return listDistrict;
		}
	
		def newplacebasic(user,address,price,buyOrRent,description,lat,lng,type,city,district,bath,bed,img){
			try {
		
//	def newplace(lat,lng,price,address,description,buyOrRent,bath,bed){
		//def place1 = new Place(lat:lat, lng:lng,price:price,address:address,description:description,buyOrRent: true,bed:bed,bath:bath)
		def place1 = new Place(lat:lat, lng:lng,price:price,address:address,description:description,buyOrRent: buyOrRent,bed:bed,bath:bath,avatar:img,status:true);
		def validateplace=place1.validate();
		if(validateplace){
			def save1=place1.save(flush: true);
			def p=PropertyType.findByNamePropertyType(type);
			place1.setPropertytype(p);
			def d=District.findByDistrictName(district);
			place1.setDistrict(d);
			def c=City.findByCityName(city);
			place1.setCity(c);
			def user1=UserProfile.findByEmail(user.email);
			place1.userprofile=user1;
			def save2=place1.save();
			if((save1)&&(save2)){
				return place1
			}
			else{
				return 0
			}
			}
		else{
		return 0
		}
				} catch (Exception ex) {
		}
		
		
	}
		def newplacefeature(place1,air,celling,elevator,laundry,parking,gagare){
			def listFeatures = []
			if(!air){}
			else {
				def f1= Feature.findByNameFeature(air);
				print f1
				listFeatures.add(f1);
			}
			if(!celling){}
			else {
				def f2= Feature.findByNameFeature(celling);
				listFeatures.add(f2);
				print f2
			}
			if(!elevator){}
			else {
				def f3= Feature.findByNameFeature(elevator);
				listFeatures.add(f3);
				print f3
			}
			if(!laundry){}
			else {
				def f4= Feature.findByNameFeature(laundry);
				listFeatures.add(f4);
				print f4
			}
			if(!parking){}
			else {
				def f5= Feature.findByNameFeature(parking);
				listFeatures.add(f5);
				print f5
			}
			if(!gagare){}
			else {
				def f6= Feature.findByNameFeature(gagare);
				listFeatures.add(f6);
				print f6
			}
			//print listFeatures
			place1.features = listFeatures as Set<Feature>
			def save3=place1.save();
			if(save3){
				return 1
				}
			else{
				return 0
				}
			
			}
		def newplaceimage(place1,img1,img2,img3){
			if(img1){
				def image1= new Image(path:img1).save(flush:true);
				image1.place=place1;
				if(!(image1.save())){
					return 0
					}
				}
			if(img2){
				def image2= new Image(path:img2).save(flush:true);
				image2.place=place1;
				if(!(image2.save())){
					return 0
					}
				}
			if(img3){
				def image3= new Image(path:img3).save(flush:true);
				image3.place=place1;
				if(!(image3.save())){
					return 0
					}
				}
			return 1
		}
		def newplacecontact(place1,namect,emailct,phonect){
			if(namect){
				def contact1=new Contact(nameContact:namect,phoneContact:phonect,emailContact:emailct)
				print contact1.validate();
				def save4=contact1.save(flush:true)
				print contact1
				place1.contact=contact1;
				def save5=place1.save();
				if(save5){
					return 1;
				}
				else{
					return 0
					}
				}
			else {
				return 0
				}
			}
}
